package com.onemile.controls;

import java.util.ArrayList;
import com.onemile.app.R;
import com.onemile.app.UICommodityList_Select;
import com.onemile.entity.PubConst;
import com.onemile.entity.Session;
import com.onemile.entity.model.ECommodity;
import com.onemile.net.NetImageLoaderM;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdpLV_CommodityList extends BaseAdapter
{

   private NetImageLoaderM imgLoader;
   private ArrayList<ECommodity> mList;
   private LayoutInflater mInflater;
   private Context mContext;

   public AdpLV_CommodityList(Context context)
   {
	  mList = new ArrayList<ECommodity>();
	  mContext = context;
	  mInflater = LayoutInflater.from(context);
	  imgLoader = new NetImageLoaderM();
   }

   public void addItem(ArrayList<ECommodity> _list)
   {
	  if (mList != null)
	  {
		 for (ECommodity eGood : _list)
		 {
			mList.add(eGood);
		 }
	  }
   }

   @Override
   public int getCount()
   {
	  if (mList == null)
	  {
		 return 0;
	  }
	  return mList.size();
   }

   @Override
   public Object getItem(int position)
   {
	  return mList.get(position);
   }

   @Override
   public long getItemId(int arg0)
   {
	  return 0;
   }

   @Override
   public View getView(int position, View currentView, ViewGroup arg2)
   {
	  try
	  {
		 ViewHolder holder;
		 if (currentView == null)
		 {
			currentView = mInflater.inflate(R.layout.commoditylist_item, null);
			holder = new ViewHolder();
			holder.CommodityName = (TextView) currentView.findViewById(R.id.txtCommodityName_commoditylist_item);
			holder.Price = (TextView) currentView.findViewById(R.id.txtPrice_commoditylist_item);
			holder.ImgCommodity = (ImageView) currentView.findViewById(R.id.imgCommodity_commoditylist_item);
			holder.ImgQuan = (ImageView) currentView.findViewById(R.id.imgQuan_commoditylist_item);
			holder.ImgJian=(ImageView)currentView.findViewById(R.id.imgJian_commoditylist_item);
			holder.imgAddToCart = (ImageView) currentView.findViewById(R.id.imgAddToCart_commoditylist_item);

			currentView.setTag(holder);
		 }
		 else
		 {
			holder = (ViewHolder) currentView.getTag();
		 }

		 if(mList.get(position).getCouponNum()==0)
		 {
			holder.ImgQuan.setVisibility(View.GONE);
		 }
		 else 
		 {
			holder.ImgQuan.setVisibility(View.VISIBLE);
		 }
		 if(mList.get(position).isIsHot())
		 {
			holder.ImgJian.setVisibility(View.VISIBLE);
		 }
		 else 
		 {
			holder.ImgJian.setVisibility(View.GONE);
		 }
		 
		 holder.imgAddToCart.setTag(mList.get(position).getCommodityId());
		 holder.ImgCommodity.setTag(mList.get(position).getCommodityId());
		 holder.CommodityName.setText(mList.get(position).getCommodityName());
		 holder.Price.setText(String.valueOf(mList.get(position).getPrice()));

		 holder.imgAddToCart.setOnClickListener(new View.OnClickListener()
		 {
			@Override
			public void onClick(View v)
			{
			   Intent intent = new Intent();
			   intent.setClass(mContext, UICommodityList_Select.class);
			   intent.putExtra(PubConst.Intent_Key.COMMODITY_ID, (String) v.getTag());
			   mContext.startActivity(intent);
			}
		 });

		 Drawable drawable = imgLoader.loadDrawable(holder.ImgCommodity, (String) holder.ImgCommodity.getTag(), 1, false, new NetImageLoaderM.ImageCallback()
		 {
			@Override
			public void setImage(ImageView imageView, Drawable imageDrawable)
			{
			   imageView.setImageDrawable(imageDrawable);
			}
		 });
		 if (drawable != null)
		 {
			holder.ImgCommodity.setImageDrawable(drawable);
		 }

		 return currentView;
	  }
	  catch (Exception e)
	  {
		 if (Session.getInstance().IsDEBUG())
		 {
			Log.e("ERROR", "AdpLV_ShopList_getView(position, currentView, arg2) ", e);
		 }
	  }
	  return new View(mContext);
   }

   static class ViewHolder
   {
	  TextView CommodityName;
	  TextView Price;
	  ImageView ImgCommodity;
	  ImageView ImgQuan;
	  ImageView ImgJian;
	  ImageView imgAddToCart;
   }

}
